<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <p>count:{{count}}</p>
      <p>doubleCount:{{doubleCount}}</p>
      <p>age:{{age}}</p>
      <p>doubleAge:{{doubleAge}}</p>
      <p>total:{{total}}</p>
      <p>totalWithAmount:{{totalWithAmount(2)}}</p>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="https://unpkg.com/vuex@3.1.3/dist/vuex.js"></script>
    <script>
      var store = new Vuex.Store({
        state:{
            count:1,
            age:18
        },
        getters:{
          doubleCount(state){
            return state.count*2;
          },
          doubleAge(state){
            return state.age*2;
          },
          total(state,getters){
            return getters.doubleCount+getters.doubleAge;
          },
          totalWithAmount(state,getters){
            return (amount)=>getters.total+amount;
          }
        }
      });
      var vm = new Vue({
        store,
        el:'#app',
        computed:{
            //用于把仓库里的状态映射为Vue实例中的计算属性
            ...Vuex.mapState(['count','age']),
            //把仓库中的getters属性映射为Vue实例中的计算属性
            ...Vuex.mapGetters(['doubleCount','doubleAge','total','totalWithAmount']),
            /* 
            doubleCount(){
              return this.$store.getters.doubleCount;
            },
            doubleAge(){
              return  this.$store.getters.doubleAge;
            },
            total(){
              return  this.$store.getters.total;
            } */
        }
      });
    </script>
  </body>
</html>